# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import os

import pytesseract
from PIL import Image
from itemadapter import ItemAdapter

# def get_real_num():
#     pytesseract.pytesseract.tesseract_cmd = 'D:\\1234\\tesseract.exe'
#     path = os.getcwd() + "ziru_num.jpg"
#     if os.path.exists(path):
#         img = Image.open('ziru_num.jpg')
#         offsets = ['-0px', '-21.4px', '-42.8px', '-64.2px', '-85.6px', '-107px', '-128.4px', '-149.8px', '-171.2px',
#                    '-192.6px']
#         num = pytesseract.image_to_string(img)
#
#         real_num = {}
#         for k, v in zip(offsets, num):
#             if v == "\n":
#                 v = "1"
#             real_num[k] = v
#         return real_num
from pymongo import MongoClient


class ZiruPipeline:
    def open_spider(self, spider):
        client = MongoClient("127.0.0.1", 27017)
        self.real_collection = client["real"]["info"]
        self.info_collection = client["info"]["info"]

    def process_item(self, item, spider):
        info_type_ = item.get("type")
        real_num = None
        if info_type_ == "real_num":
            self.real_collection.insert_many([item])

        if info_type_ == "info":
            self.info_collection.insert_many([item])
